$(function () {
    layui.use(['rate', 'layer', 'form', 'flow'], function () {
        var layer = layui.layer, form = layui.form, flow = layui.flow;
        form.render()
        // 保存考试卷
        $('.save-paper').on('click', function () {
            savePaper(layer, form)
        })
        // 收藏
        $('.coll-paper').on('click', function () {
            collectionPaper(layer)
        })
        // 问题反馈
        $('.feedback').on('click', function () {
            feedback(layer, form)
        })
        // 提交反馈
        $(document).on('click', '.feed-btn .submit-btn', function () {
            saveFeed(layer)
        })
        // 选择选项时触发左侧题号样式改变
        form.on('radio(option-check)', function (data) {
            let optionIndex = $(this).attr('attr-index') // 选中的题目序号
            $('.sub-number-list .item').each(function () {
                if (optionIndex == $(this).text()) {
                    $(this).css('background', '#f5ef3c')
                }
            })
        })
    });
})


/**
 * 提交试卷答案
 * @param layer
 * @param form
 */
function savePaper(layer, form) {
    var length = $('.subject-length').text()
    var answerEntities = []
    for (var i = 0; i < length; i++) {
        var optionId = $("input[name='subject_" + i + "']:checked").val();//每题答案
        var subjectId = $("#subjectId_" + i).val();//subjectId
        if (optionId === undefined || optionId === null) {
            optionId = -1
        }
        var answer = {subjectId: subjectId, optionId: optionId}
        answerEntities.push(answer)
    }
    form.render()
    layer.confirm('您确认交卷吗？', {
        btn: ['确认', '取消'] //按钮
    }, function () {
        var paperId = $('.paper-header .paper-id').text()
        layer.msg('试卷正在上传中...', {icon: 16, shade: 0.5});
        $.ajax({
            url: '/answer/addBatch',
            type: 'post',
            data: {answerEntities: JSON.stringify(answerEntities), paperId: paperId}, // 提交格式为json字符串
            dataType: 'json',
            success: function (response) {
                if (response.code === '000403') {
                    openLogin(layer)
                } else if (response.code === '000000') {
                    layer.msg('提交成功...', {icon: 1, time: 1000}, function () {
                        window.location.href = '/records'
                    });
                }
            }
        })
    }, function () {
        console.log('取消收藏...')
    });
}

/**
 * 收藏试卷
 * @param layer
 */
function collectionPaper(layer) {
    layer.confirm('您确认收藏该套试卷吗？', {
        btn: ['确认', '取消'] //按钮
    }, function () {
        var paperId = $('.paper-header .paper-id').text()
        $.ajax({
            url: '/collection/addSingle',
            type: 'post',
            data: {relationId: paperId, type: 2},
            dataType: 'json',
            success: function (response) {
                if (response.code === '000403') {
                    openLogin(layer)
                } else if (response.code === '000000') {
                    layer.msg('收藏成功...', {icon: 1, time: 1000}, function () {
                        location.href = ""
                    });
                }
            }
        })
    }, function () {
        console.log('取消收藏...')
    });
}

/**
 * 问题反馈
 * @param layer
 */
function feedback(layer, form) {
    layer.open({
        shade: [0.8, '#151b2d'],
        type: 1,
        anim: 1,
        area: ['30%', ''],
        content: $('#feedback-panel').html(),
        resize: false,
        move: false,
        title: false,
        yes: function (index, layero) {
            layer.close(index); //如果设定了yes回调，需进行手工关闭
        },
        success: function () {
            form.render()
        }
    });
}

/**
 * 提交问题反馈
 * @param layer
 */
function saveFeed(layer) {
    var feedType = $("input[name='feed-radio']:checked").val();//每题答案
    var content = $('#feed-content').val();
    var flag = true
    if (feedType == null || feedType == '') {
        layer.msg('问题类型不能为空', {icon: 5})
        flag = false
    }
    if (content == null || content == '') {
        layer.msg('反馈内容不能为空', {icon: 5})
        flag = false
    }
    if (flag) {
        $.ajax({
            url: '/feed/addSingle',
            type: 'post',
            data: {feedType: feedType, feedContent: content},
            dataType: 'json',
            success: function (response) {
                if (response.code === '000000') {
                    layer.msg('反馈成功...', {icon: 1, time: 1000}, function () {
                        location.href = ""
                    });
                }
            }
        })
    }
}